package com.xindaoapp.henghua.protocol;

import java.util.List;

import com.library.utils.IRequest;
import com.loopj.android.http.util.EnergyError;
import com.loopj.android.http.util.Response.ErrorListener;
import com.loopj.android.http.util.Response.Listener;
import com.xindaoapp.henghua.application.XinDaoApplication;
import com.xindaoapp.henghua.entry.PropertyGatherInfo;
import com.xindaoapp.henghua.entry.PropertyInfo;
import com.xindaoapp.henghua.entry.PropertyLabelInfo;
import com.xindaoapp.henghua.entry.PropertyWithLabelDataInfo;
import com.xindaoapp.henghua.entry.ScanData;
import com.xindaoapp.henghua.entry.ScandataInfo;
import com.xindaoapp.henghua.entry.SuperVisionInfo;
import com.xindaoapp.henghua.entry.TagsInfo;
import com.xindaoapp.henghua.entry.TransportDataInfo;

public class HttpManager {
	private IRequest<List<PropertyGatherInfo>> iRequest;

	private final String propertyVersion;
	private final String transportDatasVersion;
	private final String supervisionVersion;
	private final String scanDatasVersion;
	private final String propertyWithLabelDataVersion;
	private final String projectId;
	private final String arriveDataVersion;

	public HttpManager(String propertyVersion, String transportDatasVersion, String supervisionVersion,
			String scanDatasVersion, String propertyWithLabelDataVersion, String tagsVersion,
			String arriveDataVersion, String projectId) {
		super();
		this.propertyVersion = propertyVersion;
		this.transportDatasVersion = transportDatasVersion;
		this.supervisionVersion = supervisionVersion;
		this.scanDatasVersion = scanDatasVersion;
		this.propertyWithLabelDataVersion = propertyWithLabelDataVersion;
		this.arriveDataVersion = arriveDataVersion;
		this.projectId = projectId;
	}

	public HttpManager(String projectId) {
		super();
		this.propertyVersion = "";
		this.transportDatasVersion = "";
		this.supervisionVersion = "";
		this.scanDatasVersion = "";
		this.propertyWithLabelDataVersion = "";
		this.arriveDataVersion = "";
		this.projectId = projectId;
	}

	/**
	 * 初始化所有信息内容
	 * 
	 * @param iRequest
	 */
	public void getPropertyGatherInfos(final IRequest<List<PropertyGatherInfo>> iRequest) {
		this.iRequest = iRequest;
		// 首先请求所有信息，建立Map
		XinDaoApplication.get().getMoccaApi()
				.getPropertyDatas(propertyVersion, projectId, new Listener<PropertyInfo>() {

					@Override
					public void onResponse(PropertyInfo response) {

						if (null == response) {
							iRequest.request(null);
							return;
						}

						for (PropertyGatherInfo info : response.content) {
							XinDaoApplication.get().getDBHelper().updatePropertyInfo(info);
						}
						// 更新数据
						/*if (isUpdate) {
							list.addAll(response.content);
						} else {
							list = response.content;
						}*/

						/*for (PropertyGatherInfo info : list) {
							map.put(info.id, info);
						}*/

						// 调用其他接口，进行数据初始化
						initPropertyWithLabelDataInfo();
					}
				}, new ErrorListener() {

					@Override
					public void onErrorResponse(EnergyError error) {
						iRequest.request(null);
					}
				});
	}

	protected void initTransportDatas() {
		XinDaoApplication.get().getMoccaApi()
				.gettransportDatas(transportDatasVersion, projectId, new Listener<List<TransportDataInfo>>() {

					@Override
					public void onResponse(List<TransportDataInfo> response) {
						if (null == response) {
							iRequest.request(null);
							return;
						}

						// 更新数据
//						if (isUpdate) {
						for (TransportDataInfo info : response) {
							XinDaoApplication.get().getDBHelper().updateTransportData(info);
						}
//						}

						/*for (TransportDataInfo info : response) {
							if (map.containsKey(info.assert_id)) {
								map.get(info.assert_id).transportDataInfo = info;
							}
						}*/
						initTags();
					}
				}, new ErrorListener() {

					@Override
					public void onErrorResponse(EnergyError error) {
						iRequest.request(null);
					}
				});
	}

	private void initSupervision() {
		XinDaoApplication.get().getMoccaApi()
				.getSuperVision(supervisionVersion, projectId, new Listener<List<SuperVisionInfo>>() {

					@Override
					public void onResponse(List<SuperVisionInfo> response) {
						if (null == response) {
							iRequest.request(null);
							return;
						}

						// 更新数据
//						if (isUpdate) {
						for (SuperVisionInfo info : response) {
							XinDaoApplication.get().getDBHelper().updateSupervision(info);
						}
//						}

						/*for (SuperVisionInfo info : response) {
							if (map.containsKey(info.assert_id)) {
								map.get(info.assert_id).superVisionInfo = info;
							}
						}*/
						initTransportDatas();
					}
				}, new ErrorListener() {

					@Override
					public void onErrorResponse(EnergyError error) {
						iRequest.request(null);
					}
				});
	}

	/**
	 * 服务器读取数据，写入到sqlite，然后读sqlite到内存
	 */
	private void initScanDatas() {
		XinDaoApplication.get().getMoccaApi()
				.getScanDatas(scanDatasVersion, projectId, new Listener<List<ScandataInfo>>() {

					@Override
					public void onResponse(List<ScandataInfo> response) {
						if (null == response) {
							iRequest.request(null);
							return;
						}

						// 初始化每条数据的assertid
						for (ScandataInfo info : response) {
							for (ScanData scanData : info.data) {
								scanData.assert_id = info.assert_id;
							}
						}

						// 更新数据
//						if (isUpdate) {
						for (ScandataInfo info : response) {
							for (ScanData scandataInfo : info.data) {
								XinDaoApplication.get().getDBHelper().updateScanData(scandataInfo);
							}
						}
//						}
						/*for (ScandataInfo info : response) {
							// 首先判断这条数据是否已经存在
							if (map.containsKey(info.assert_id)) {
								// TODO
								map.get(info.assert_id).scanDatas.addAll(info.data);
							}
						}*/
						initSupervision();
					}
				}, new ErrorListener() {

					@Override
					public void onErrorResponse(EnergyError error) {
						iRequest.request(null);
					}
				});
	}

	private void initPropertyWithLabelDataInfo() {
		XinDaoApplication
				.get()
				.getMoccaApi()
				.getPropertyWithLabelDatas(propertyWithLabelDataVersion, projectId,
						new Listener<List<PropertyWithLabelDataInfo>>() {

							@Override
							public void onResponse(List<PropertyWithLabelDataInfo> response) {
								if (null == response) {
									iRequest.request(null);
									return;
								}

								// 初始化每条数据的assertid
								for (PropertyWithLabelDataInfo info : response) {
									for (PropertyLabelInfo propertyWithLabelDataInfo : info.data) {
										propertyWithLabelDataInfo.assert_id = info.assert_id;
									}
								}

								// 更新数据
//								if (isUpdate) {
								for (PropertyWithLabelDataInfo info : response) {
									for (PropertyLabelInfo propertyWithLabelDataInfo : info.data) {
										XinDaoApplication.get().getDBHelper()
												.updatePropertyWithLabelDataInfo(propertyWithLabelDataInfo);
									}
								}
//								}

								/*for (PropertyWithLabelDataInfo info : response) {
									if (map.containsKey(info.assert_id)) {
										// TODO
										map.get(info.assert_id).propertyLabelInfos.addAll(info.data);
									}
								}*/
								initScanDatas();
							}
						}, new ErrorListener() {

							@Override
							public void onErrorResponse(EnergyError error) {
								iRequest.request(null);
							}
						});
	}

	private void initTags() {
		XinDaoApplication.get().getMoccaApi().getTags("", new Listener<List<TagsInfo>>() {

			@Override
			public void onResponse(List<TagsInfo> response) {
				if (null == response) {
					iRequest.request(null);
					return;
				}
				XinDaoApplication.get().getUpdateDataChanged().setTagsInfos(response);

				if (response.size() > 0) {
					XinDaoApplication.get().getDBHelper().clearTags();
					// 缓存到数据库
					for (TagsInfo tagsInfo : response) {
						XinDaoApplication.get().getDBHelper().insertTags(tagsInfo);
					}
				}

				// 回调所有数据
				updateDatas();
			}
		}, new ErrorListener() {

			@Override
			public void onErrorResponse(EnergyError error) {
				iRequest.request(null);
			}
		});
	}

	/**
	 * 从数据库读取数据，进行更新
	 */
	public void updateDatas() {
		iRequest.request(XinDaoApplication.get().getDBHelper().getPropertyGatherInfos());
	}

}
